package cn.kgc.tangcco.listener;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.kgc.tangcco.utils.localdatetime.BaseLocalDateTimeUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cglib.core.Local;

import javax.servlet.ServletRequest;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * @author 李昊哲
 * @Description
 * @create 2020/10/19 下午4:35
 */
public class RequestListener implements ServletRequestListener {
    public RequestListener() {
        System.out.println("phoenix");
    }

    @Override
    public void requestDestroyed(ServletRequestEvent sre) {

    }

    @Override
    public void requestInitialized(ServletRequestEvent sre) {
        HttpServletRequest request = (HttpServletRequest) sre.getServletRequest();
        String remoteAddr = request.getRemoteAddr();
        String time = BaseLocalDateTimeUtil.format(LocalDateTime.now());
        StringBuffer requestURL = request.getRequestURL();
        String queryString = request.getQueryString();
        if (StringUtils.isBlank(queryString)) {
            System.out.println(time + "\t" + remoteAddr + "\t" + requestURL);
        } else {
            System.out.println(time + "\t" + remoteAddr + "\t" + requestURL + "?" + queryString);
        }
    }
}
